Flask tutorial login problem

by: glennf, 8 years ago


I have gone through lesson 18 - AWESOME learning experience, thank you! I cannot get my login to work - the sign up works fine, adds the user to the database and all is well - when i go to login it says invalid credentials, try again.
i have tried with several users - I am wondering if the password is in error because I had trouble with the secret code and added the app.secret_key = 'mysecretkey' to the _init_.py  - i also set the secret key in the flaskapp.wsgi file - I think I matched the keys but.... it's not working -- any ideas or guidance is appreciated - is it possible to get a copy of the _init_.py from the tutorial to check against my file?? Thank you again!



You must be logged in to post. Please login or register an account.



I do not recall if this is an exact match, but: https://github.com/PythonProgramming/n8n.r6siege.cn-Website

I do not expect your issue is key related. When you set it in the __init__.py file you should be all set from there. If you're developing locally, you could just try removing the wsgi file to test. If you're on an actual server, you want to be using the WSGI file, not having it locally. From your other post, I am assuming you're running locally at the moment.

-Harrison 8 years ago
Last edited 8 years ago

You must be logged in to post. Please login or register an account.


Thank you for the reply and your generosity - I'll dig in and try to sort it out looking at the example - do you make more $$ on hats or shirts?

-glennf 8 years ago

You must be logged in to post. Please login or register an account.


well I'm still stumped ---- my _init_.py for login is:
def login_page():
    error = ''

    try:
error = None
c, conn = connection()
if request.method == "POST":

data = c.execute("SELECT * FROM tbl_user WHERE user_username = (%s)",
thwart(request.form['username']))

data = c.fetchone()[3]

flash (data)

if sha256_crypt.verify(request.form['password'], data):
session['logged_in'] = True
session['username'] = request.form['username']

flash("You are now logged in")
return redirect(url_for("dashboard"))

else:
error = "(1)Invalid credentials, try again."

gc.collect()

return render_template("login.html", error=error)

    except Exception as e:
#flash(e)
error = "(2)Invalid credentials, try again."
return render_template("login.html", error = error)

I have changed the table name, field name and the number of fetchone field- all verified with the database in MySQL which is:
Table: tbl_user
Columns:
user_id int(11) AI PK
user_name varchar(255)
user_username varchar(255)
user_password varchar(100)

I did the select query from the command line and got the correct row of data.

when I press the login button is get "(2)Invalid credentials, try again." and flash :"not all arguments converted during string formatting "

I had a similar problem in the register section which was remedied by adding the comma after (thwart(username) in the code as seen below:
x = c.execute("SELECT * FROM tbl_user WHERE user_username = (%s)",
                          (thwart(username),))
Not sure exactly why that worked but some site recommended it and it worked.

I have tried that in various ways on the code for login ("data = c.execute("SELECT * FROM tbl_user WHERE user_username = (%s)", thwart(request.form['username']))") but no success.

Any guidance is appreciated.


-glennf 8 years ago

You must be logged in to post. Please login or register an account.